package com.gdth.bank.activity.setting;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.bank.StringParam;
import com.gdth.bank.adapter.CjkhFjWscListViewAdapter;
import com.gdth.bank.adapter.CjkhFjWscListViewAdapter.OnCjjlDetailClickListener;
import com.gdth.bank.presenter.CjkhFjxxPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.view.CjkhFjxxView;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.model.Item;
import com.gdth.utils.ToolUtil;
import com.gdth.widget.dialog.BaseToast;

public class BfkhFjActivity  extends BaseActivity implements CjkhFjxxView{
	@ViewInject(id = R.id.header_label_title)
	TextView txt_header_title;
	@ViewInject(id = R.id.header_btn_left, click = "finishClick")
	TextView txt_header_left;
	@ViewInject(id = R.id.header_btn_right, click = "addClick")
	TextView txt_header_right;
	@ViewInject(id=R.id.qtxx_gridview, itemClick="onItemClick") ListView listView_fjxx;
	/** 提示框 */
	private ProgressDialog mProgressDialog;
	CjkhFjxxPresenter presenter;
	private List<TAppFjxxb> fjxxList;
	private CjkhFjWscListViewAdapter adapter;
	List<Item> fjlxList = new ArrayList<Item>();
	String positionId="";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fjwsc_list);
		initView();
		
		initfj();
	}


	private void initfj() {
		// TODO Auto-generated method stub
		presenter.getFjxxByUuid("");
	}


	private void initView() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		txt_header_title.setText(intent.getStringExtra(getResources().getString(R.string.param9)));
		txt_header_right.setText(R.string.param8);
		txt_header_right.setBackground(null);
		presenter = new CjkhFjxxPresenter(this);
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setCancelable(false);
		fjxxList = new ArrayList<TAppFjxxb>();
		adapter = new CjkhFjWscListViewAdapter(this, fjxxList);
		fjlxList = AppUtil.getCanshuByType(this, AppUtil.SHARE_DATA_FJLX);
		adapter.setFjlx(fjlxList);
		listView_fjxx.setAdapter(adapter);
		adapter.setOnCjjlDetailClickListener(new OnDetailClickListener());
	}
	class OnDetailClickListener implements OnCjjlDetailClickListener {
		@Override
		public void clickListener(int position) {
			detail(position);
		}
	}
	private void detail(int position) {
//		Toast.makeText(BfkhFjActivity.this, position+"", Toast.LENGTH_LONG).show();
		positionId = position+"";
		TAppFjxxb mTAppFjxxb=fjxxList.get(position);
		String uuid=mTAppFjxxb.getUuid();
//		presenter.getFjxxuuid(uuid);
		presenter.panduanWl(uuid);
	}

	@Override
	public void saveSuccess(Long id) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void getAllFjxx(List<TAppFjxxb> arg0) {
		// TODO Auto-generated method stub
		fjxxList.clear();
		for (TAppFjxxb mTAppFjxxb : arg0) {
			String wjlx=mTAppFjxxb.getWjmc();
			String wjlxx = wjlx.substring(wjlx.lastIndexOf(".") + 1, wjlx.length());
			mTAppFjxxb.setWjlx(wjlxx);
//			mTAppFjxxb.setAppdz());
			String filePath =ToolUtil.getFilePath(mTAppFjxxb.getUuid() +"."+wjlxx);
			if(!filePath.startsWith(Environment.getExternalStorageDirectory().toString())){
				filePath = Environment.getExternalStorageDirectory() + filePath;
			}
	         File file = new File(filePath);
	         if(file.exists()) {
	        	 fjxxList.add(mTAppFjxxb);
	         }else{
	         	String dzString = mTAppFjxxb.getAppdz();
				 if(!dzString.startsWith(Environment.getExternalStorageDirectory().toString())){
					 dzString = Environment.getExternalStorageDirectory() + dzString;
				 }
				 File file1 = new File(dzString);
				 if(file1.exists()){
					 fjxxList.add(mTAppFjxxb);
				 }
			 }
		}
		
		updateGridView();
	}
	public void updateGridView(){
		adapter.notifyDataSetChanged();
	}


	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}


	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		mProgressDialog.dismiss();
	}


	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		BaseToast.getInstance().ShowSystemToast(this, msg);
	}


	@Override
	public void saveSuccess(int fjSize) {
		// TODO Auto-generated method stub
		if(fjSize==0){
		fjxxList.remove(Integer.parseInt(positionId));
		updateGridView();
		}else if(fjSize==1){
			presenter.getFjxxByUuid("");
			updateGridView();
		}
	}
	public void addClick(View arg0){
		if(null == fjxxList || 0 == fjxxList.size()) {
			showMessage(getResources().getString(R.string.param10));
			return;
		}
		
		new AlertDialog.Builder(BfkhFjActivity.this).setTitle(getResources().getString(R.string.param11))//设置对话框标题
	     .setMessage(getResources().getString(R.string.param12))//设置显示的内容
	     .setPositiveButton(getResources().getString(R.string.param13),new DialogInterface.OnClickListener() {//添加确定按钮
	         @Override  
	         public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件  
	             // TODO Auto-generated method stub  
	        	 if(fjxxList.size()>0){
	        			presenter.panduanWl("");
	        			}else{
	        				showMessage(getResources().getString(R.string.param14));
	        			}
	         }  
	  
	     }).setNegativeButton(getResources().getString(R.string.param15),new DialogInterface.OnClickListener() {//添加返回按钮
	         @Override  
	         public void onClick(DialogInterface dialog, int which) {//响应事件  
	             // TODO Auto-generated method stub  
	         }  
	  
	     }).show();
		
	}
//	public boolean panduanwl(){
//		ConnectivityManager conMgr= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//		NetworkInfo activeInfo= conMgr.getActiveNetworkInfo();
//		if(activeInfo !=null && activeInfo.isConnected()){
//			int netSubtype=activeInfo.getType();
//			if(netSubtype == ConnectivityManager.TYPE_MOBILE){
//				return false;
//			}
//		}
//		return false;
//		 
//	}


	
}
